package com.soul.factory.defaults;

import com.soul.factory.Factory;
import com.soul.factory.FactoryException;
import com.soul.factory.RegistFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 默认注册工厂实现
 */
public final class RegistFactoryImpl implements RegistFactory {
    
    private static Map<String,Factory> registMap = new HashMap<>();
    
    private static RegistFactory registFactory = new RegistFactoryImpl();
    
    private RegistFactoryImpl(){
    }
    
    public static RegistFactory getInstance() {
        return registFactory;
    }

    @Override
    public void regist(Factory factory) {
        Factory fac = registMap.get(factory.name());
        if (fac != null){
            throw new FactoryException("name 为‘" + fac.name() + "’的工厂已存在");
        }
        registMap.put(factory.name(),factory);
    }

    @Override
    public Factory get(String name) {
        if (null != name){
            return registMap.get(name);
        }
        return null;
    }


}
